package com.game.config.gameserver;

import com.game.config.XMLConfig;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * Created by legend9999 on 2016/7/4.
 */
@Component
@Root(name = "config")
public class GameServerConfig extends XMLConfig {
	private Logger logger = LoggerFactory.getLogger(GameServerConfig.class);
	@ElementList(name = "GameServers", required = false, type = GameServer.class, entry = "GameServer")
	private List<GameServer> list;
	@Element
	private RedisServer redisServer;
	@Element
	private MysqlServer mysqlServer;
	private static GameServerConfig instance;

	@PostConstruct
	public void init() {
		try {
			logger.info("load GameServerConfig");
			instance = serializer.read(this.getClass(),this.getFile());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static GameServerConfig getInstance() {
		return instance;
	}

	public static void main(String[] args) {
		/*GameServerConfig gameServerConfig = new GameServerConfig();
		List<GameServer> list = new ArrayList<GameServer>();
		GameServer gs = new GameServer();
		gs.setIp("fdsa");
		gs.setPort(121);
		gs.setGameName("fdsa");
		gs.setGameId(1);
		list.add(gs);
		list.add(gs);
		list.add(gs);
		list.add(gs);
		list.add(gs);
		list.add(gs);
		RedisServer rs = new RedisServer();
		rs.setIp("32131");
		rs.setPort(342);
		MysqlServer ms = new MysqlServer();
		ms.setIp("fdsa");
		ms.setPort(342);
		ms.setPassowrd("fdsa");
		ms.setUsername("432");
		gameServerConfig.list = list;
		gameServerConfig.redisServer = rs;
		gameServerConfig.mysqlServer = ms;
		Serializer serializer = new Persister();
		try {
			serializer.write(gameServerConfig, gameServerConfig.getFile());
			GameServerConfig read = serializer.read(GameServerConfig.class, gameServerConfig.getFile());
			System.out.println();
		} catch (Exception e) {
			e.printStackTrace();
		}*/
		ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
		GameServerConfig instance = GameServerConfig.getInstance();
		System.out.println();
	}

	protected String getDic() {
		return "game-server";
	}

	protected String getFileName() {
		return "GameServerConfig";
	}

	public List<GameServer> getList() {
		return list;
	}

	public RedisServer getRedisServer() {
		return redisServer;
	}

	public MysqlServer getMysqlServer() {
		return mysqlServer;
	}
}
